<?php
/**
 * 问答
 * User: E 房cms www.fangcms.com
 * Date: 2015/1/20
 * Time: 10:51
 */
namespace Home\Controller;
use Org\Yike\FangCms;
class AskController extends CommonController{
    public function _initialize(){
        parent::_initialize();
        $this->ask_keys = explode(',',$this->site['ask_keys']);
    }

    public function index(){
        if($_GET['key']){
            $key = urldecode(trim($_GET['key']));
            if($key==''){
                $this->error('找不到对应的搜索词信息');
            }
            $FangCms = new FangCms();
            $this->ask_cate = $FangCms->getCate('ask_cate',2);
            $this->page_arr = $FangCms->getAskList(25,array('a.status'=>1,'a.title'=>array('like','%'.$key.'%')),true);
            $this->solve_status = solveStatus();
            $this->v=$key;
            $this->seoKey(array('title'=>$key));
            $this->display('so');
        }else{
            $FangCms = new FangCms();
            $this->ask_cate = $FangCms->getCate('ask_cate',2);
            //热门分类
            $this->top_cate = M('ask_cate')->where(array('status'=>1,'pid'=>array('neq',0)))->order('rand()')->limit(5)->select();

            //获取问答数最多的房型
            $house_info = M('house_info')->field('house_id,ask')->order('ask desc')->limit(8)->select();
            $house_ids = $house_ask = array();
            foreach($house_info as $v){
                $house_ids[] = $v['house_id'];
                $house_ask[$v['house_id']] = $v['ask'];
            }
            $this->house_list = M('house')->where(array('id'=>array('in',$house_ids)))->field('id,title,img')->select();
            $this->house_ask = $house_ask;

            //回答之星
            $this->user_answer = M('user_info')->order('answer desc')->limit(3)->select();
            $this->list = $FangCms->getAskList(8);
            $this->solve_status = solveStatus();
            $this->display();
        }
    }

    public function lists(){
        $FangCms = new FangCms();
        $this->ask_cate = $FangCms->getCate('ask_cate',1);
        $cate = $FangCms->getCateOne('ask_cate',array('status'=>1,'alias'=>trim($_GET['alias'])));
        $this->cate_nav = $FangCms->currentLocation('ask_cate',array('status'=>1,'id'=>$cate['id']));

        //分类选择
        $cate_list = M('ask_cate')->where(array('status'=>1,'pid'=>0))->order('ordid desc,id asc')->select();
        $this->pid = $cate['pid'] ? $cate['pid'] : $cate['id'];
        $this->cate_ids = M('ask_cate')->where(array('status'=>1,'pid'=>$this->pid))->order('ordid desc,id asc')->select();

        $where = array('status'=>1);
        if($solve = intval($_GET['solve'])){
            $where['solve_status'] = $solve;
            $this->solve = $solve;
        }
        if(!$cate['pid']){
            //获取所有分类下的ID
            $ask_ids = array();
            foreach($this->cate_ids as $v){
                $ask_ids[] = $v['id'];
            }
            $where['cate_id'] = array('in',implode(',',$ask_ids));
        }else{
            $where['cate_id'] = $cate['id'];
        }
        $this->cate_for = cateFor($this->ask_cate);
        //按条件查询
        $this->page_arr = getPageShow('ask',$where,30);
        //最新问答
        $this->ask_new = M('ask')->where($where)->order('id desc')->limit(9)->select();
        //最热问答
        $this->ask_hits = M('ask')->where($where)->order('hits desc')->limit(9)->select();
        $this->cate_list = $cate_list;
        $this->solve_status = solveStatus();
        $this->cate = $cate;
        $this->seoKey($cate,'name');
        $this->display();
    }

    //问答详细
    public function detail(){
        $FangCms = new FangCms();
        $this->ask_cate = $FangCms->getCate('ask_cate',1);
        $this->ask_cate_list = $FangCms->getCate('ask_cate',2);
        $id = intval($_GET['id']);
        if(!$id){
            $this->error('没有找到对应ID信息');
        }
        $info = M('ask')->where(array('id'=>$id,'status'=>1))->find();
        if(!$info['id']){
            $this->error('没有找到对应ID信息');
        }
        $FangCms->setInc($id,'ask');
        //相关楼盘
        if($info['house_id']){
            $this->house = M('house')->where(array('id'=>$info['house_id'],'status'=>1))->find();
        }

        //当前位置
        $this->cate_nav = $FangCms->currentLocation('ask_cate',array('status'=>1,'id'=>$info['cate_id']));
        $info['user'] = $info['user_id']? getUserInfo($info['user_id']) : '游客';
        $this->cate = M('ask_cate')->where(array('id'=>$info['cate_id']))->find();
        $this->page_arr = $FangCms->getAnswerList($info['id'],5);

        //相关问题
        $this->rec_list = M('ask')->where(array('status'=>1,'cate_id'=>$info['cate_id'],'id'=>array('gt',$info['id'])))->order('id asc')->limit(8)->select();

        $this->info = $info;
        $this->seoKey($info,'title');
        $this->display();
    }

    //最新问题
    public function problem(){
        $FangCms = new FangCms();
        $this->ask_cate = $FangCms->getCate('ask_cate',2);
        $this->page_arr = $FangCms->getAskList(25,array('a.status'=>1,'a.solve_status'=>1),true);
        $this->solve_status = solveStatus();
        $this->seoKey(array('title'=>'最新问答,最新咨询,最新问题'));
        $this->display();
    }

    //最新解决
    public function answer(){
        $FangCms = new FangCms();
        $this->ask_cate = $FangCms->getCate('ask_cate',2);
        $this->page_arr = $FangCms->getAskList(25,array('a.status'=>1,'a.solve_status'=>2),true);
        $this->solve_status = solveStatus();
        $this->seoKey(array('title'=>'最新解决'));
        $this->display();
    }

    public function add(){
        $FangCms = new FangCms();
        $this->ask_cate = $FangCms->getCate('ask_cate',1);
        $this->ask_cate_list = $FangCms->getCate('ask_cate',2);
        $this->seoKey(array('title'=>'在线问答'));
        $this->display();
    }

}